package com.swanclouds.redfish.chassis;

import com.google.common.base.Preconditions;
import com.swanclouds.api.chassis.DrivesService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.common.ActionResponse;
import com.swanclouds.model.common.redfish.CollectionResource;
import com.swanclouds.redfish.chassis.domain.DrivesModel;
import com.swanclouds.redfish.common.redfish.CollectionResourceModel;
import com.swanclouds.redfish.common.redfish.IndicatorLED;
import com.swanclouds.redfish.internal.BaseChassisService;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: HuangFuYuHui
 * Title: DrivesServiceImpl
 * Description: TODO
 * Date: 2022/7/19 15:33
 */
public class DrivesServiceImpl extends BaseChassisService implements DrivesService {

    /**
     * 查询硬盘集合资源信息
     * @return  CollectionResource
     **/
    @Override
    public CollectionResource get() {
        return get(CollectionResourceModel.class,uri(ClientConstants.PATH_CHASSIS_DRIVES,getChassisId())).execute();
    }

    /**
     * 查询指定服务器硬盘集合资源信息
     * @param driverId 服务器硬盘Id
     * @return com.swanclouds.model.chassis.Drives
     **/
    public DrivesModel getDrive(String driverId) {
        Preconditions.checkNotNull(driverId, "driverId is null");
        return get(DrivesModel.class, uri(ClientConstants.PATH_CHASSIS_DRIVES_ID, getChassisId(), driverId)).execute();
    }

    /**
     * 修改服务器指定驱动器的属性。
     * @param indicatorLED indicatorLED属性
     * @param driverId 驱动器Id
     * @return com.swanclouds.model.common.ActionResponse
     **/
    public ActionResponse updateDriveAttribute(IndicatorLED indicatorLED, String driverId) {
        Preconditions.checkNotNull(indicatorLED, "indicatorLED is null");
        Preconditions.checkNotNull(driverId, "driverId is null");
        return  patch(ActionResponse.class,uri(ClientConstants.PATH_CHASSIS_DRIVES_ID,getChassisId(),driverId))
                .entity(indicatorLED)
                .execute();
    }

    /**
     * 查询服务器硬盘集合资源信息
     * @return java.util.List
     **/
    @Override
    public List<DrivesModel> list() {
        List<DrivesModel> drivesModels = new ArrayList<DrivesModel>();
        try {
            CollectionResource resource = get();
            resource.getCollectionResourceList().stream().forEach(ref -> {
                drivesModels.add(get(DrivesModel.class, uri(ref)).execute());
            });
        }catch (Exception e){
            e.printStackTrace();
        }
        return drivesModels;
    }
}
